/*
 * Package il.ac.biu.cs.grossmm.test.test2
 * File FooToPidf.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.tests.test2;

import static il.ac.biu.cs.grossmm.api.data.NodeTypeByInterface.*;
import il.ac.biu.cs.grossmm.api.OperationFailedException;
import il.ac.biu.cs.grossmm.api.data.NodeType;
import il.ac.biu.cs.grossmm.api.data.Root;
import il.ac.biu.cs.grossmm.api.flow.ActiveDataManager;
import il.ac.biu.cs.grossmm.api.flow.Converter;
import il.ac.biu.cs.grossmm.api.flow.ConverterProxy;
import il.ac.biu.cs.grossmm.api.flow.NodeEventInspector;
import il.ac.biu.cs.grossmm.api.flow.NodeInspector;
import il.ac.biu.cs.grossmm.api.flow.NodeManipulator;
import il.ac.biu.cs.grossmm.api.presence.Pidf;
import il.ac.biu.cs.grossmm.api.server.Component;
import il.ac.biu.cs.grossmm.api.server.ComponentManager;
import il.ac.biu.cs.grossmm.api.server.ServiceManager;
import il.ac.biu.cs.grossmm.api.server.Service;

import java.util.logging.Logger;

public class PidfToFoo2 implements Converter<Pidf, FooPresence>, Component,
	Service {
	Logger logger = Logger.getLogger("FooToPidf2");

	static class Proxy implements ConverterProxy<Pidf, FooPresence> {
		Root<Pidf> pidf;
		Root<FooPresence> fooPresence;

		public Proxy(Root<Pidf> srcNode, Root<FooPresence> dstNode, NodeInspector srcInspector,
			NodeManipulator dstManipulator) throws OperationFailedException
		{
			this.fooPresence = dstNode;
			this.pidf = srcNode;
			convert(srcInspector, dstManipulator);
		}

		public void convert(NodeEventInspector srcInspector,
			NodeManipulator dstManipulator) throws OperationFailedException
		{
			convert((NodeInspector)srcInspector, dstManipulator);
		}

		/**
		 * @param srcInspector
		 * @param dstManipulator
		 * @throws OperationFailedException
		 */
		private void convert(NodeInspector srcInspector, NodeManipulator dstManipulator) throws OperationFailedException
		{
			String entity = srcInspector.getValue(pidf, Pidf.ENTITY); 
			dstManipulator.setValue(fooPresence, FooPresence.ENTITY, entity);
			
			dstManipulator.setValue(fooPresence, FooPresence.IS_ONLINE, true);
		}

		public void release()
		{
			// TODO Implement release
			// 
			throw new RuntimeException("Not implemented");
		}

	}

	public ConverterProxy<Pidf, FooPresence> createProxy(Root<Pidf> srcNode, Root<FooPresence> dstNode, NodeInspector srcInspector, NodeManipulator dstManipulator) throws OperationFailedException
	{
		return new Proxy(srcNode, dstNode, srcInspector, dstManipulator);
	}

	public NodeType<Pidf> getSourceType()
	{
		return nodeType(Pidf.class);
	}

	public double getPriority()
	{
		return 0.2;
	}

	public NodeType<FooPresence> getDestinationType()
	{
		return nodeType(FooPresence.class);
	}

	public void aborted(Object hint, Exception e) throws Exception
	{
		// TODO Implement aborted
		// 
		throw new RuntimeException("Not implemented");
	}

	public void setComponentManager(ComponentManager container)
		throws Exception
	{
		container.getByClass(ActiveDataManager.class).registerConverter(this);
	}

	public void start(ServiceManager serviceManager) throws Exception
	{
		logger.info("started");
	}

	public void stop(Exception exception)
	{
		logger.info("stopped");
	}

	public void terminated(Object serviceHint, Exception e) throws Exception
	{
		// TODO Implement terminated
		// 
		throw new RuntimeException("Not implemented");
	}

}
